package com.woniuxy.consume.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.woniuxy.consume.fallback.GetB;
import com.woniuxy.consume.fallback.GetF;
import com.woniuxy.consume.feign.ProductFeign;
import com.woniuxy.consume.service.ConsumeService;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RefreshScope
public class ConsumeController {
    @Autowired
    private RestTemplate restTemplate;//远程调用 http请求

    @Resource
    private ProductFeign productFeign;

    @RequestMapping("/conf/{msg}")
    public String con(@PathVariable String msg){

        //调用product服务
        //String url="http://localhost:8080/getProduct/"+msg;
//        String url="http://spring-product/getProduct/"+msg;
//        String rest = restTemplate.getForObject(url, String.class);

        //调用方法
        String rest = productFeign.getProduct(msg);
        return "con"+msg +" "+rest;
    }

    @Value("${aa}")
    private String aa;

    @RequestMapping("/aa")
    public String aam(){
        return aa;
    }

    @Autowired
    private ConsumeService consumeService;

    @RequestMapping("/add")
    public String add(){
        return "add" +consumeService.todo();
    }

    @RequestMapping("/get")
    @SentinelResource(value = "/gettodo",
    fallback = "getF",fallbackClass = GetF.class
//    blockHandler = "getB",blockHandlerClass = GetB.class
    )
    public String get(){
        return "get" + consumeService.todo();
    }



    @RequestMapping("/work")
    public void work(){
        String url1="http://spring-account/updateMoneyByid/1/900";
        String url2="http://spring-storage/updateNumById/1/9";
        String url3="http://spring-order/addOrder";
        restTemplate.getForObject(url1, String.class);
        restTemplate.getForObject(url2,String.class);
        restTemplate.getForObject(url3,String.class);
    }

}
